package cn.com.taiji.oospub.wechat.common.util;

import cn.com.taiji.oospub.wechat.common.MySSLProtocolSocketFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class HttpUtil {
    //请求的boss地址
    private String requestURL;

    //链接超时时间 5000
    private int connectionTimeout = 60000;
    //请求超时时间
    private int timeout = 60000;

    public HttpUtil() {

    }

    public HttpUtil(String requestURL) {
        this.requestURL = requestURL;
    }


    /**
     * 向pos发送请求
     *
     * @throws
     */
    public String sendToBoss(String xml) throws Exception {
        long begin = System.currentTimeMillis();
        //记录日志
        System.out.println("1------------------");
        HttpClient client = new HttpClient();
        String result = "";
        //通过HTTPS请求时候配置的
        System.out.println("2------------------");
        Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
        System.out.println("3------------------");
        Protocol.registerProtocol("https", myhttps);
        System.out.println("4------------------");
        //通过HTTPS请求时候配置的
        PostMethod method = new PostMethod(requestURL);
        System.out.println("5------------------");
        method.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        System.out.println("6------------------");
        //服务器 超时设置
        method.setRequestHeader("Connection", "close");
        System.out.println("7------------------");
        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(xml.getBytes());
//        ByteArrayRequestEntity entity = new ByteArrayRequestEntity(xml.getBytes(StandardCharsets.UTF_8));
        System.out.println("8------------------");
        try {
            method.setRequestEntity(entity);
            client.setConnectionTimeout(connectionTimeout);
            client.setTimeout(timeout);
            client.executeMethod(method);
            BufferedInputStream bis = new BufferedInputStream(method.getResponseBodyAsStream());
            //log.info("response code:" + method.getStatusCode()+"response message:"+method.getStatusText());
            ByteArrayOutputStream bop = new ByteArrayOutputStream();
            int i = -1;
            while ((i = bis.read()) != -1) {
                bop.write(i);
            }
            result = bop.toString("utf-8");
        } catch (HttpException e) {
//			log.error("httpclient执行executeMethod时候HttpException异常"+e);
            e.printStackTrace();
            throw new Exception("00001");
        } catch (IOException e) {
//			log.error("httpclient IOException异常"+e);
            e.printStackTrace();
            throw new Exception("00002");
        } catch (Exception e) {
//			log.error("httpclient Exception异常"+e);
            e.printStackTrace();
            throw new Exception("00003");
        } finally {
            //释放链接
            method.releaseConnection();
//			//加上关闭连接
            client.getHttpConnectionManager().closeIdleConnections(0);
//			((SimpleHttpConnectionManager)client.getHttpConnectionManager()).shutdown();  
            if (result != null && !"".equals(result)) {
                result = result.replaceAll("  ", "");
            }
        }

        return result;
    }
}
